//
//  FYCollectionViewCell.m
//  YiQiTingShu
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 直到世界的尽头. All rights reserved.
//

#import "FYCollectionViewCell.h"
#import "PublicHeader.h"
#import "WHNCollectionViewCell.h"
#import "FYPageScrollView.h"
#import <UIImageView+WebCache.h>
#import "FCollectionReusableView.h"
#import "FCollectionViewCell.h"
#import <MBProgressHUD.h>

#define Location [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"data"]

@interface FYCollectionViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property(nonatomic, strong)UICollectionView *collectionView;
@property(nonatomic, strong)NSMutableArray *dataSource;
@property(nonatomic, strong)NSString *trackId;
@property(nonatomic, strong)NSString *title;
@property(nonatomic, strong)MBProgressHUD *progressView;

@end
static NSString *normalCell = @"normal";
static NSString *longCell = @"long";
static NSString *labelCell = @"label";
static NSString *systemCell = @"system";
static NSString *header = @"header";

@implementation FYCollectionViewCell

-(UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.sectionInset = UIEdgeInsetsMake(2, 15, 15, 15);
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:self.frame collectionViewLayout:flowLayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView NightWithType:UIViewColorTypeNormal];
        _collectionView.bounces = NO;
        [_collectionView registerClass:[FCollectionViewCell class] forCellWithReuseIdentifier:normalCell];
        [_collectionView registerClass:[WHNCollectionViewCell class] forCellWithReuseIdentifier:longCell];
        [_collectionView registerClass:[WHNCollectionViewCell class] forCellWithReuseIdentifier:labelCell];
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:systemCell];
        [_collectionView registerClass:[FCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:header];
    }
    return _collectionView;
}

#pragma mark 加载图
-(MBProgressHUD *)progressView
{
    if (!_progressView) {
        _progressView = [[MBProgressHUD alloc]initWithView:self];
        _progressView.labelText = @"加载中";
        [_progressView setMode:MBProgressHUDModeIndeterminate];
        [_progressView show:YES];
        [_progressView NightWithType:UIViewColorTypeNormal];
    }
    return _progressView;
}

-(void)initData
{
    _dataSource = [NSMutableArray array];
    [AFTools getDataByURLString:@"http://mobile.ximalaya.com/mobile/discovery/v1/recommends?channel=ios-b1&device=iPhone&includeActivity=true&includeSpecial=true&scale=2&version=4.3.20" WithDataBlock:^(id data) {
//        NSString *string = Location;
//        [data writeToFile:string atomically:YES];
//        [data writeToFile:string atomically:YES encoding:NSUTF8StringEncoding error:nil];
        //焦点图(数组)
        [_dataSource addObject:[[data objectForKey:@"focusImages"] objectForKey:@"list"]];
        //小编推荐(数组)
        [_dataSource addObject:[[data objectForKey:@"editorRecommendAlbums"] objectForKey:@"list"]];
        //精品听单(数组)
        [_dataSource addObject:[[data objectForKey:@"specialColumn"] objectForKey:@"list"]];
        //热门推荐(字典)
        [_dataSource addObjectsFromArray:[[data objectForKey:@"hotRecommends"] objectForKey:@"list"]];
        [_collectionView reloadData];
        [_progressView removeFromSuperview];
    }];
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.collectionView];
        [self initData];
        [self addSubview:self.progressView];
    }
    return self;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    if (_dataSource.count == 0) {
        return 0;
    }
    return 15;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    if (section == 2) {
        return 2;
    }
    return 3;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //焦点图
    if (indexPath.section == 0) {
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:systemCell forIndexPath:indexPath];
        if (cell.subviews.count == 1) {
            NSMutableArray *array = [NSMutableArray array];
            for (NSDictionary *each in _dataSource[indexPath.section]) {
                [array addObject:[each objectForKey:@"pic"]];
            }
            FYPageScrollView *view = [[FYPageScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, FitConstant_YorHeight(200)) PicArray:array];
            [cell addSubview:view];
        }
        return cell;
    }
    //小编推荐
    else if (indexPath.section == 1){
        NSDictionary *dic = _dataSource[indexPath.section][indexPath.row];
        FCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:normalCell forIndexPath:indexPath];
        cell.types = FCollectionViewCellTypeRecommend;
        [cell.mainImageView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"coverLarge"]] placeholderImage:[UIImage imageNamed:@"placeholderZh"]];
        cell.titleLabel.text = [dic objectForKey:@"trackTitle"];
        cell.subTitle.text = [dic objectForKey:@"tags"];
        return cell;
    }
    //精品听单
    else if (indexPath.section == 2){
        WHNCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:longCell forIndexPath:indexPath];
        cell.kind = WHNCollectionViewKindThreeLabelAndThreePicture;
        NSDictionary *dic = _dataSource[indexPath.section][indexPath.row];
        [cell.mainImageView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"coverPath"]] placeholderImage:[UIImage imageNamed:@"placeholderZh"]];
        cell.titleLabel.text = [dic objectForKey:@"title"];
        cell.byLabel.text = [dic objectForKey:@"subtitle"];
        cell.numLabel.text = [dic objectForKey:@"footnote"];
        
        return cell;
    }
    
    NSDictionary *dic = [[_dataSource[indexPath.section] objectForKey:@"list"] objectAtIndex:indexPath.row];
    FCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:normalCell forIndexPath:indexPath];
    cell.types = FCollectionViewCellTypeRecommend;
    [cell.mainImageView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"coverLarge"]] placeholderImage:[UIImage imageNamed:@"placeholderZh"]];
    cell.titleLabel.text = [dic objectForKey:@"trackTitle"];
    cell.subTitle.text = [dic objectForKey:@"tags"];
    
    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //焦点图
    if (indexPath.section == 0) {
        return CGSizeMake(self.frame.size.width, FitConstant_YorHeight(180));
    }
    //精品听单
    else if (indexPath.section == 2){
        return CGSizeMake(self.frame.size.width, FitConstant_YorHeight(110));
    }
    //小编推荐--热门推荐
    return CGSizeMake(FitConstant_XorWidth(100), FitConstant_YorHeight(130));
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    //焦点图
    if (section == 0) {
        return CGSizeMake(0, 0);
    }
    return CGSizeMake(self.frame.size.width, FitConstant_YorHeight(40));
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    FCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:header forIndexPath:indexPath];
    view.type = FCollectionReusableViewTypesMore;
    
    
    if (indexPath.section == 0 || indexPath.section == 1) {
        NSDictionary *dic = [_dataSource[indexPath.section] objectAtIndex:indexPath.row];
        _trackId = [NSString stringWithFormat:@"%@", [dic objectForKey:@"trackId"]];
        _title = @"小编";
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        view.assistLabel.userInteractionEnabled = YES;
        [view.assistLabel addGestureRecognizer:tap];
        view.titleLabel.text = @"小编推荐";
        
        return view;
    }
    else if (indexPath.section == 2){
        view.titleLabel.text = @"精品听单";
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction1)];
        view.assistLabel.userInteractionEnabled = YES;
        [view.assistLabel addGestureRecognizer:tap];
        return view;
    }
    
    //小编推荐--热门推荐
    view.titleLabel.text = [_dataSource[indexPath.section] objectForKey:@"title"];
    _trackId = [NSString stringWithFormat:@"%@", [_dataSource[indexPath.section] objectForKey:@"categoryId"]];
    _title = [_dataSource[indexPath.section] objectForKey:@"title"];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    view.assistLabel.userInteractionEnabled = YES;
    [view.assistLabel addGestureRecognizer:tap];
    
    return view;
}

-(void)tapAction
{
    self.block(_trackId, _title);
}

-(void)tapAction1
{
    self.block(@"-1", _title);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return;
    }
    else if (indexPath.section == 1){
        NSDictionary *dic = [_dataSource[indexPath.section] objectAtIndex:indexPath.row];
        [self.delegate sendTitle:[dic objectForKey:@"title"] TrackID:[dic objectForKey:@"trackId"] AlbumID:[dic objectForKey:@"albumId"]];
        return;
    }
    else if (indexPath.section == 2){
        NSDictionary *dic = [_dataSource[indexPath.section] objectAtIndex:indexPath.row];
        NSInteger specialId = [[dic objectForKey:@"specialId"] integerValue];
        [self.delegate sendID:specialId];
        return;
    }
    
    //小编推荐--热门推荐
    NSDictionary *dic = [[_dataSource[indexPath.section] objectForKey:@"list"] objectAtIndex:indexPath.row];
    [self.delegate sendTitle:[dic objectForKey:@"title"] TrackID:[dic objectForKey:@"trackId"] AlbumID:[dic objectForKey:@"albumId"]];
}



@end
